<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- import CSS --><!--地址改成从cdn服务器 稳定性高-->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>

<div>
    <input type="text" placeholder="说点啥吧" v-model="weibo.content">
    <el-upload
            action="/upload"
            name="picFile"
            limit="1"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button @click="insert()">发表微博</el-button>
</div>
</body>
<!--引入Axios-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{content:"",url:""}
            }
        },
        methods:{
            insert(){
                if (v.weibo.url==""){
                    alert("请选择上传的图片");
                    return;
                }
                axios.post("/insert",v.weibo).then(function () {
                    alert("发表成功")
                    location.href = "/";
                })
            },
            handleSuccess(response,file,fileList){
                alert(response)
                v.weibo.url = response;
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                v.weibo.url="";
                axios.post("/delete?url="+file.response).then(function () {

                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        }
    })
</script>
</html>